<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/6/27 9:56
 * Modified Time: 2019/6/27 9:56
 * Version: 2.0
 */

namespace App\Modules\Service\Services;


use App\Exceptions\AllValidatorException;
use App\Lib\Utilities;
use App\Modules\Employee\Repositories\EmployeeRoyaltyRepository;
use App\Modules\Employee\Repositories\PortfolioRepository;
use App\Modules\Service\Criterias\GetServiceTypeAndServiceCriteria;
use App\Modules\Service\Criterias\StoreServiceCriteria;
use App\Modules\Service\Repositories\CardTypeServiceRepository;
use App\Modules\Service\Repositories\ServiceRepository;
use App\Modules\System\Repositories\DictionaryItemRepository;
use App\Modules\System\Repositories\UserStoreRepository;
use Flower\Core\Repository\Eloquent\BaseService;
use Illuminate\Support\Facades\DB;

class ServiceService extends BaseService
{
	private $serviceRepository;
	private $userStoreRepository;
	private $portfolioRepository;
	private $cardTypeServiceRepository;
	private $dictionaryItemRepository;
	private $employeeRoyaltyRepository;

	public function __construct(ServiceRepository $serviceRepository,
	                            UserStoreRepository $userStoreRepository,
	                            PortfolioRepository $portfolioRepository,
	                            CardTypeServiceRepository $cardTypeServiceRepository,
	                            DictionaryItemRepository $dictionaryItemRepository,
	                            EmployeeRoyaltyRepository $employeeRoyaltyRepository)
	{
		$this->serviceRepository = $serviceRepository;
		$this->userStoreRepository = $userStoreRepository;
		$this->portfolioRepository = $portfolioRepository;
		$this->cardTypeServiceRepository = $cardTypeServiceRepository;
		$this->dictionaryItemRepository = $dictionaryItemRepository;
		$this->employeeRoyaltyRepository = $employeeRoyaltyRepository;
	}

	/**
	 * 添加服务
	 * @param $store_id
	 * @param $name
	 * @param $price
	 * @param $type
	 * @param $royalty
	 * @param $cost
	 * @param $assistant_royalty
	 * @return mixed
	 * @throws AllValidatorException
	 */
	public function createService($store_id, $name, $price, $type, $royalty, $cost, $assistant_royalty)
	{

		//检测本店有无同名服务
		$this->checkServiceName($store_id, $name);

		DB::beginTransaction();

		try {
			$new_service = $this->serviceRepository->createService($store_id, $name, $price, $type, $royalty, $cost, $assistant_royalty);

			//为理发师设置提成
			$barber = $this->userStoreRepository->findByField(['sid' => $store_id])->toArray();
			$service_id = $new_service->id;
			foreach ($barber as $key => $value) {

				$this->employeeRoyaltyRepository->setRoyalty($value['uid'], $store_id, $service_id, $royalty, 0);

			}

			DB::commit();

			return $new_service;


		} catch (AllValidatorException $allValidatorException) {
			DB::rollBack();
			throw new AllValidatorException(['msg' => $allValidatorException->msg]);
		}

	}

	/*
	 * 获取本店所有服务
	 */
	public function getServiceList($store_id)
	{

		$service = $this->serviceRepository->pushCriteria(new StoreServiceCriteria($store_id))->get();

		return $service;

	}

	/**
	 * 获取本店所有服务分页
	 */
	public function getServiceListPage($store_id, $service_type, $pageSize)
	{

		$service = $this
			->serviceRepository
			->pushCriteria(new StoreServiceCriteria($store_id, $service_type))
			->paginate($pageSize, ['*']);

		$service = Utilities::dealPage($service);
		return $service;
	}

	/*
	 * 删除服务
	 */
	public function delService($service_id)
	{

		$this->couldDelete($service_id);
		DB::beginTransaction();

		try {
			$this->serviceRepository->delService($service_id);
			$this->employeeRoyaltyRepository->delService($service_id);
			return DB::commit();
		} catch (AllValidatorException $allValidatorException) {
			DB::rollBack();
			throw new AllValidatorException(['msg' => $allValidatorException->msg]);
		}
	}

	/**
	 * 判断能否删除服务
	 * @param $service_id
	 * @throws AllValidatorException
	 */
	protected function couldDelete($service_id)
	{
		//判断有无关联作品
		$portfolio_count = $this->portfolioRepository->getCountByService($service_id);
		if ($portfolio_count > 0) {
			throw new AllValidatorException(['msg' => '店内有' . $portfolio_count . '个作品与该服务关联，暂不可删除。']);
		}
		//判断有无关联会员卡
		$card_type_count = $this->cardTypeServiceRepository->getCardTypeCountByService($service_id);
		if ($card_type_count > 0) {
			throw new AllValidatorException(['msg' => '店内有' . $card_type_count . '个会员卡类与该服务关联，暂不可删除。']);
		}
		return true;
	}

	/**
	 * 编辑服务
	 * @param $store_id
	 * @param $service_id
	 * @param $name
	 * @param $price
	 * @param $type
	 * @param $royalty
	 * @param $cost
	 * @param $assistant_royalty
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function editService($store_id, $service_id, $name, $price, $type, $royalty, $cost, $assistant_royalty)
	{
		//判断有没有修改服务名
		if ($name) {
			$this->checkServiceName($store_id, $name, $service_id);
		}

		return $this->serviceRepository->editService($service_id, $name, $price, $type, $royalty, $cost, $assistant_royalty);
	}

	/*
	 * 检测本店有无同名服务
	 * $service_id ：服务id  默认为空 这样编辑服务时可以跳过本身
	 */
	private function checkServiceName($store_id, $name, $service_id = null)
	{
		$where = [
			['store_id', '=', $store_id],
			['name', '=', $name],
			['id', '<>', $service_id]
		];
		$service_num = $this->serviceRepository->findByField($where)->count();

		if ($service_num > 0) {
//            throw new ValidatorException(new MessageBag(['本店已有同名服务哦']));
			throw new AllValidatorException(['msg' => '本店已有同名服务哦']);
		}

	}

	/**
	 * 服务是否下架
	 * @param $serviceId
	 * @return mixed
	 */
	public function isOff($serviceId)
	{
		$res = $this->serviceRepository->find($serviceId);
		return empty($res) ? true : false;
	}

	public function getServiceTypeAndService($store_id, $barber_id)
	{
		return $this->dictionaryItemRepository->pushCriteria(new GetServiceTypeAndServiceCriteria($store_id, $barber_id))->get();
	}
}